This is a search engine friendly export of a TiddlyWiki. Please click here for the actual site.

TiddlyWiki Export Script


This is just a little Python script that I hacked together to export tiddlers into a more search engine-friendly format. It simply exports each tiddler to a tiddly directory and creates an index page with links to all of them. I am aware that there is a TiddlyWiki Export plugin, but it doesn't seem to do exactly the same thing and this was easy to add to my web server as a cron job every night to automatically keep the export up to date.

Note that everything is hardcoded (server address, output directory - which is relative to where you run the script, and so on) so if you want to use it for your site some changes will be needed. It's also probably full of bugs so use at your own risk. I'm not responsible for anything that happens as a result of using this script.

With that out of the way, here it is:
{{{
#!/usr/bin/env python

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2009 Ben Nemec

import sys
import re
import os

def unhtml(s):
retval = s.replace("/", "_")
retval = retval.replace("&quot;", "_")
retval = retval.replace("?", "_")
retval = retval.replace("&gt;", ">")
retval = retval.replace("&lt;", "<")
return retval

def makeahref(matchobj):
original = matchobj.group()
if original[:2] != "[[":
value = "<a href=\"tiddly/" + original + ".html\">" + original + "</a>"
return value
original = original[2:-2]
if original.find("|") == -1:
value = unhtml(original)
value = "<a href=\"tiddly/" + value + ".html\">" + original + "</a>"
return value
else:
values = original.split("|")
value = "<a href=\"" + values[1] + "\">" + values[0] + "</a>"
return value

if not os.path.exists("tiddly"):
os.mkdir("tiddly")

tfile = open(sys.argv[1])
index = open("tiddly/index.html", 'w')
instore = False

# This was just for debugging, obviously
#linkify = re.compile("(?:(.*)\[\[(.*)\]\])*(.*)")
linkify = re.compile("\[\[(.*)\]\]|[A-Z][A-Z]+[a-z]+\w*|[A-Z]+[a-z]+[A-Z]+\w*")

index.write("<html>\n")
index.write("<head>\n")
index.write("<title>Nemebean.com TiddlyWiki Export</title>\n")
index.write("</head>\n")
index.write("<body>\n")
index.write("<h1>Index</h1>\n")
index.write("This is an index into the TiddlyWiki export for Nemebean.com. Please <a href=\"/tiddly.html\">click here</a> for the actual site.<br />\n")
index.write("<br/>\n")

for i in tfile:
currline = i.rstrip("\n")

if currline.find("<!--POST-STOREAREA-->") != -1:
instore = False

if instore:
if currline[:4] == "<div":
currtitle = currline.split("\"")[1]
filetitle = unhtml(currtitle)
currfile = open("tiddly/" + filetitle + ".html", 'w')

currfile.write("<html>\n")
currfile.write("<head>\n")
currfile.write("<base href=\"http://www.nemebean.com\"/>")
currfile.write("<meta name=\"robots\" content=\"nofollow\"/>")
currfile.write("<title>" + currtitle + "</title>\n")
currfile.write("</head>\n")
currfile.write("<body>\n")
currfile.write("This is a search engine friendly export of a TiddlyWiki. Please click <a href=\"/tiddly.html#" + currtitle + "\">here</a> for the actual site.<br />\n")
currfile.write("<h2>" + currtitle + "</h2>\n")
currfile.write("<br/>\n")

index.write("<a href=\"" + filetitle + ".html\">" + currtitle + "</a><br/>\n")
elif not currfile.closed:
if currline[:5] == "</div":
currfile.write("</body>\n")
currfile.write("</html>\n")
currfile.close()
else:
results = linkify.sub(makeahref, currline)
results = results.replace("<pre>", "")
results = results.replace("</pre>", "")
currfile.write(results + "<br/>\n")


if currline.find("<div id=\"storeArea\">") != -1:
instore = True

index.write("</body>\n")
index.write("</html>\n")
index.close()

}}}